package main

import (
	"fmt"
	"github.com/jinzhu/gorm"
	_ "github.com/jinzhu/gorm/dialects/mysql"
	"time"
)

func main() {
	db, err := gorm.Open("mysql", "root:Admin@123@(82.157.142.228)/myf?charset=utf8mb4&parseTime=True&loc=Local")
	if err != nil {
		panic(err)
	}
	defer db.Close()
	// 自动迁移生成表
	db.AutoMigrate(&UserInfo{})
	db.AutoMigrate(&User{})

	u1 := UserInfo{1, "七米", "男", "篮球"}
	u2 := UserInfo{2, "沙河娜扎", "女", "足球"}
	// 创建记录
	db.Create(&u1)
	db.Create(&u2)
	// 查询
	var u = new(UserInfo)
	db.First(u)
	fmt.Printf("%#v\n", u)

	var uu UserInfo
	db.Find(&uu, "hobby=?", "足球")
	fmt.Printf("%#v\n", uu)

	// 更新
	db.Model(&u).Update("hobby", "双色球")
	// 删除
	db.Delete(&u)
}

// UserInfo 用户信息
type UserInfo struct {
	ID     uint64 `gorm:"primary_key"`
	Name   string
	Gender string
	Hobby  string
}

// 将 `ID`, `CreatedAt`, `UpdatedAt`, `DeletedAt`字段注入到`User`模型中
type User struct {
	gorm.Model
	Name string
}

//	employee
//	@Description: CREATE TABLE `employee` (
//	`id` bigint NOT NULL AUTO_INCREMENT COMMENT '主键id',
//	`id_card` varchar(20) NOT NULL COMMENT '身份证号码',
//	`name` varchar(64) NOT NULL COMMENT '姓名',
//	`age` int NOT NULL COMMENT '年龄',
//	`city` varchar(64) NOT NULL COMMENT '城市',
//	PRIMARY KEY (`id`)
//
// ) ENGINE=InnoDB AUTO_INCREMENT=15 DEFAULT CHARSET=utf8mb3 COMMENT='员工表';
type employee struct {
	Id     int    `json:"id"`
	IdCard string `json:"id_card"`
	Name   string `json:"name"`
	Age    int    `json:"age"`
	City   string `json:"city"`
}

// gorm.Model 定义
type Model struct {
	ID        uint `gorm:"primary_key"`
	CreatedAt time.Time
	UpdatedAt time.Time
	DeletedAt *time.Time
}
